--- 战斗主类
---
--- @class SimulatorService
SimulatorService =  ClientFight.CreateClass("SimulatorService")
local this = SimulatorService
--- @public
--- @return SimulatorService
    --private static final Logger log = LoggerFactory.getLogger(SimulatorService:class);
    --protected static final Logger log = LoggerFactory.getLogger(SimulatorService.class);
    ---@type SimulatorService
    this.instance = this;
    ---@type table<number, HeroFightLog>
    this.logs = {};
    ---@type table<string, TeamFightLog>
    this.teamLos = {};


function this:getInstance()
    return this.instance;
end
function this.getHeroFightLog(fighter)
    local heroFightLog = logs[fighter.heroBean.f_HeroID];
    if (heroFightLog == nil) then
        heroFightLog = ClientFight.HeroFightLog.New();
        heroFightLog.heroId = fighter.heroBean.f_HeroID;
        -- heroFightLog.setMonsterTempleteId(fighter.getMonsterTempletId());
        logs[fighter.heroBean.f_HeroID] = heroFightLog;
    end
    return heroFightLog;
end

function this.getTeamLog(teamStr)
    local log = teamLos[teamStr];
    if (log == nil) then
        log = TeamFightLog.New();
        teamLos[teamStr] = log;
    end
    return log;
end

function this.writeLogs(times)
    --    local dir = "fightcsvs/";
    --    local file = new File(dir);
    --    if (!file.exists()) {
    --    file.mkdir();
    --    }
    --
    --    long fileName = os.time();
    --    OutputStreamWriter fileWritter = new OutputStreamWriter(new FileOutputStream(dir + fileName .. ".csv"), StandardCharsets.UTF_8);
    --    OutputStreamWriter heroFileWritter = new OutputStreamWriter(new FileOutputStream(dir + fileName .. "_hero" .. ".csv"), StandardCharsets.UTF_8);
    --
    --    logs.forEach((heroId, log) -> {
    --    StringBuilder sb = new StringBuilder();
    --    sb.append("heroId").append(",").append(heroId).append(",").append("fightTimes").append(",").append(log.getFightTimes()).append(",").append("winTimes").append(",").append(log.getWinIimes()).append(",").append("attack").append(",").append(log.getFightStatisticsVal(FightStatisticsEnum.ATTACK) / log.getFightTimes()).append(",").append("heal").append(",").append(log.getFightStatisticsVal(FightStatisticsEnum.DO_HEAL) / log.getFightTimes()).append(",").append("injured").append(",").append(log.getFightStatisticsVal(FightStatisticsEnum.BE_INJURED) / log.getFightTimes());
    --
    --    try {
    --    heroFileWritter.write(sb.toString());
    --    heroFileWritter.write("\r\n");
    --    } catch (IOException e) {
    --    e.printStackTrace();
    --    }
    --    });
    --
    --    teamLos.forEach((teamStr, log) -> {
    --    -- 平均毫秒数
    --    long second = log.getTotalFrame() * (Global.FIGHT_FRAME) / times;
    --    StringBuilder sb = new StringBuilder();
    --    sb.append(teamStr).append(",").append("leftWinTimes").append(",").append(log.getTeam0WinTimes()).append(",").append("rightWinTimes").append(",").append(log.getTeam1WinTimes()).append(",").append("second").append(",").append(second);
    --    try {
    --    fileWritter.write(sb.toString());
    --    fileWritter.write("\r\n");
    --    } catch (IOException e) {
    --    e.printStackTrace();
    --    }
    --    });
    --    heroFileWritter.close();
    --    fileWritter.close();
    --    }
end
function this. run()
    local times = 1;-- Integer.parseInt(args.getOptionValues("times").get(0));
    local frozen = false;-- Integer.parseInt(args.getOptionValues("frozen").get(0)) == 1;

    local beanAmount = table.getn(ClientFight.DataManagerTest.t_gladiatorteamBeanList);
    for i = 1, beanAmount do
        for j = i + 1, beanAmount do
            local teamLog = this.getTeamLog(i .. "--" .. j);
            for kk = 1, times do
                local battleInfo = ClientFight.SimulatorManager.createBattleInfo(os.time(), i, j, math.random());
                local fight = ClientFight.FightClientManager.init(battleInfo,false);
                fight.teams[1].frozen = frozen;

                ClientFight.FightClientManager.start(fight);

                teamLog.fightTimes = teamLog.fightTimes + 1;
                teamLog.totalFrame = fight.frame + teamLog.totalFrame;
                local winIndex = 0;
                if (fight.finish == 2) then
                    -- 0方胜利
                    teamLog.team0WinTimes = teamLog.team0WinTimes + 1;
                else
                    winIndex = 1;
                    teamLog.team1WinTimes = teamLog.team1WinTimes + 1;
                end

                for k = 1, table.getn(fight.teams) do
                    local tmpTeam = fight.teams[k];
                    local fighterList = clone(tmpTeam.fighters);
                    addTable(fighterList,tmpTeam.alternates) ;
                    for i, f in pairs(fighterList) do

                        local fightLog = this.getHeroFightLog(f);
                        fightLog:addFightStatistics(FightStatisticsEnum.ATTACK, f.fighterStats.attack);
                        fightLog:addFightStatistics(FightStatisticsEnum.DO_HEAL, f.fighterStats.heal);
                        fightLog:addFightStatistics(FightStatisticsEnum.BE_INJURED, f.fighterStats.beInjured);
                        fightLog.fightTimes = fightLog.fightTimes + 1;
                        if (f.teamIndex == winIndex) then
                            fightLog.winIimes = fightLog.winIimes + 1;
                        end
                    end
                end
                if ( fight.debug) then
                --    org.apache.logging.log4j.Logger logger = Log4j2Util.getLogger("fights/", String.valueOf(fight.getFightId()));
                --logger.info(JSON.toJSONString(fight.getActions(), SerializerFeature.DisableCircularReferenceDetect));
                --Log4j2Util.stop(String.valueOf(fight.getFightId()));
                end
            end
        end
    end

    writeLogs(times);
    --log.info("战斗演算结束");
end
